﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Kugar.Core.ExtMethod;
using Kugar.Core.Log;
using Kugar.Tools.IM.Tencent.ServerSDK;
using Newtonsoft.Json.Linq;

namespace Kugar.Tools.IM.Tencent.ServerTest
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

           LoggerManager.LoggerFactory=new NLogFactory();

            LoggerManager.Default.Debug("服务器启动");

            TencentIMApi.MessageNotify += TencentIM_MessageNotify;
            TencentIMApi.UserOnlineStatusChanged += TencentIM_UserOnlineStatusChanged;
        }

        private void TencentIM_UserOnlineStatusChanged(string accountID, bool isOnlineNow, ServerSDK.Enums.UserOnlineStatusChangeReasonEnum reason)
        {
            LoggerManager.Default.Debug($"UserStatus:AccountID={accountID};isOnlineNow={isOnlineNow};reason={reason.GetDescription()}");
        }

        private void TencentIM_MessageNotify(int senderType, DateTime sendDt, string fromAccount, string toAccount, IMMessageBase[] msgBody)
        {
            LoggerManager.Default.Debug($"MessageLog:Date:{sendDt:yyyy-MM-dd HH:mm:ss};SendAccountID={fromAccount};RevAccountID={toAccount};Body={JArray.FromObject(msgBody)}");
        }
    }
}
